<?php
    if (!defined('IN_IPB')) {
        print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
        exit();
    }
    //echo "sssss";exit();
    class register_home{
        var $output = "";
        var $page_title = "";
        var $nav = array();
        var $html = "";
        var $email = "";
        var $modules = "";
        function auto_run(){
            
            //-----------------------------------------
            // Require the HTML and language modules
            //-----------------------------------------
            $this->ipsclass->load_language("lang_genaral");
            $this->ipsclass->load_language('lang_register');
            $this->ipsclass->load_template('skin_register');
            
            $this->base_url = $this->ipsclass->base_url;
            $this->base_url_nosess = "{$this->ipsclass->vars['board_url']}/index.{$this->ipsclass->vars['php_ext']}";

            //-----------------------------------------
            // Fix up languages
            //-----------------------------------------

            foreach (array('dname_text', 'reg_error_dname_len', 'reg_error_username_none', 'reg_error_no_name', 'user_name_text') as $k) {
                $this->ipsclass->lang[$k] = sprintf($this->ipsclass->lang[$k], $this->ipsclass->vars['max_user_name_length']);
            }

            //-----------------------------------------
            // Get the emailer module
            //-----------------------------------------

            require ROOT_PATH . "sources/classes/class_email.php";

            $this->email = new emailer();
            $this->email->ipsclass = & $this->ipsclass;
            $this->email->email_init();

            if (USE_MODULES == 1) {
                require ROOT_PATH . "modules/ipb_member_sync.php";

                $this->modules = new ipb_member_sync();
                $this->modules->ipsclass = & $this->ipsclass;
            }

            //-----------------------------------------
            // Board offline?
            //-----------------------------------------

            if ($this->ipsclass->vars['board_offline'] == 1) {
                if ($this->ipsclass->member['g_access_offline'] != 1) {
                    $this->ipsclass->vars['no_reg'] = 1;
                }
            }

            $this->ipsclass->vars['username_characters'] = str_replace('"', '\"', $this->ipsclass->vars['username_characters']);

            //-----------------------------------------
            // What to do?
            //-----------------------------------------

            switch ($this->ipsclass->input['CODE']) {
                case '02':
                    $this->create_account();
                    break;

                case '03':
                    $this->validate_user();
                    break;

                case '05':
                    $this->show_manual_form();
                    break;

                case '06':
                    $this->show_manual_form('lostpass');
                    break;

                case 'lostpassform':
                    $this->show_manual_form('lostpass');
                    break;

                case '07':
                    $this->show_manual_form('newemail');
                    break;

                case '10':
                    $this->lost_password_start();
                    break;
                case '11':
                    $this->lost_password_end();
                    break;

                case '12':
                    $this->coppa_perms_form();
                    break;
                case '16':

                    $this->lost_password();
                    break;
                case 'coppa_two':
                    $this->coppa_two();
                    break;

                case 'image':
                    $this->show_image();
                    break;

                case 'reval':
                    $this->revalidate_one();
                    break;

                case 'reval2':
                    $this->revalidate_two();
                    break;

                case 'complete_login':
                    $this->complete_login_form();
                    break;
                case 'complete_login_do':
                    $this->complete_login_save();
                    break;
                case 'termsread':// Read Terms
                    $this->show_terms_read();
                    break;
                case 'quickregister':
                    $this->show_terms_read("", 1);
                    break;
                default:
                    if ($this->ipsclass->vars['use_coppa'] == 1 and $this->ipsclass->input['coppa_pass'] != 1) {
                        $this->coppa_start();
                    } else {
                        $this->show_terms_read();
                    }
                    break;
            }

            //-----------------------------------------
            // If we have any HTML to print, do so...
            //-----------------------------------------

            $this->ipsclass->print->add_output("$this->output");
            $this->ipsclass->print->do_output(array('TITLE' => $this->page_title, 'JS' => 0, 'NAV' => $this->nav));
        }
        function show_reg_form($form_errors = array()) {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $final_errors = array();

            if ($this->ipsclass->vars['no_reg'] == 1) {
                $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'reg_off'));
            }

            $coppa = $this->ipsclass->my_getcookie('coppa');
            //$coppa = (isset($this->ipsclass->input['coppa_user']) AND $this->ipsclass->input['coppa_user'] == 1) ? 1 : 0;

            if ($coppa == 'yes') {
                $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'awaiting_coppa'));
            }

            $this->ipsclass->vars['username_errormsg'] = str_replace('{chars}', $this->ipsclass->vars['username_characters'], $this->ipsclass->vars['username_errormsg']);

            //-----------------------------------------
            // Read T&Cs yet?
            //-----------------------------------------

            if (!isset($this->ipsclass->input['termsread']) OR !$this->ipsclass->input['termsread']) {
                if ($this->ipsclass->member['id']) {
                    //-----------------------------------------
                    // Log member out
                    //-----------------------------------------

                    require_once( ROOT_PATH . 'sources/action_public/login.php' );
                    $login = new login();
                    $login->ipsclass = & $this->ipsclass;
                    $login->do_log_out(0);
                }

                //-----------------------------------------
                // Continue
                //-----------------------------------------

                $cache = $this->ipsclass->DB->simple_exec_query(array('select' => '*', 'from' => 'conf_settings', 'where' => "conf_key='reg_rules'"));

                $text = $cache['conf_value'] ? $cache['conf_value'] : $cache['conf_default'];

                $this->page_title = $this->ipsclass->lang['registration_form'];
                $this->nav = array($this->ipsclass->lang['registration_form']);

                $this->output .= $this->ipsclass->compiled_templates['skin_register']->show_terms($this->ipsclass->my_nl2br($text), $coppa);
                return;
            } else {
                //-----------------------------------------
                // Did we agree to the t&c?
                //-----------------------------------------

                if (!$this->ipsclass->input['agree_to_terms']) {
                    $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'reg_no_agree', 'EXTRA' => $this->ipsclass->base_url));
                }
            }

            //-----------------------------------------
            // Do we have another URL that one needs
            // to visit to register?
            //-----------------------------------------

            $this->login_method = $this->ipsclass->DB->build_and_exec_query(array('select' => '*', 'from' => 'login_methods', 'where' => 'login_enabled=1'));

            if ($this->login_method['login_register_url']) {
                $this->ipsclass->boink_it($this->login_method['login_register_url']);
                exit();
            }

            //-----------------------------------------
            // Continue...
            //-----------------------------------------

            if ($this->ipsclass->vars['reg_auth_type']) {
                if ($this->ipsclass->vars['reg_auth_type'] == 'admin_user' OR $this->ipsclass->vars['reg_auth_type'] == 'user') {
                    $this->ipsclass->lang['std_text'] .= "<br />" . $this->ipsclass->lang['email_validate_text'];
                }

                //-----------------------------------------
                // User then admin?
                //-----------------------------------------

                if ($this->ipsclass->vars['reg_auth_type'] == 'admin_user') {
                    $this->ipsclass->lang['std_text'] .= "<br />" . $this->ipsclass->lang['user_admin_validation'];
                }

                if ($this->ipsclass->vars['reg_auth_type'] == 'admin') {
                    $this->ipsclass->lang['std_text'] .= "<br />" . $this->ipsclass->lang['just_admin_validation'];
                }
            }

            //-----------------------------------------
            // Clean out anti-spam stuffy
            //-----------------------------------------

            if ($this->ipsclass->vars['bot_antispam']) {
                // Set a new ID for this reg request...

                $regid = md5(uniqid(microtime()));

                if ($this->ipsclass->vars['bot_antispam'] == 'gd') {
                    //-----------------------------------------
                    // Get 6 random chars
                    //-----------------------------------------

                    $reg_code = strtoupper(substr(md5(mt_rand()), 0, 6));
                } else {
                    //-----------------------------------------
                    // Set a new 6 character numerical string
                    //-----------------------------------------

                    $reg_code = mt_rand(100000, 999999);
                }

                // Insert into the DB

                $this->ipsclass->DB->do_insert('reg_antispam', array(
                    'regid' => $regid,
                    'regcode' => $reg_code,
                    'ip_address' => $this->ipsclass->input['IP_ADDRESS'],
                    'ctime' => time(),
                ));
            }

            //-----------------------------------------
            // Custom profile fields stuff
            //-----------------------------------------

            $required_output = "";
            $optional_output = "";

            require_once( ROOT_PATH . 'sources/classes/class_custom_fields.php' );
            $fields = new custom_fields($this->ipsclass->DB);

            $fields->cache_data = $this->ipsclass->cache['profilefields'];

            $fields->init_data();
            $fields->parse_to_register();

            foreach ($fields->out_fields as $id => $data) {
                $error = "";

                if ($fields->cache_data[$id]['pf_not_null'] == 1) {
                    $ftype = 'required_output';
                } else {
                    $ftype = 'optional_output';
                }

                if (isset($form_errors['cfield_' . $id]) AND count($form_errors['cfield_' . $id])) {
                    $error = implode("<br />", $form_errors['cfield_' . $id]);
                }

                if ($fields->cache_data[$id]['pf_type'] == 'drop') {
                    $form_element = $this->ipsclass->compiled_templates['skin_register']->field_dropdown('field_' . $id, $data, $error);
                } else if ($fields->cache_data[$id]['pf_type'] == 'area') {
                    $data = $this->ipsclass->input['field_' . $id] ? $this->ipsclass->input['field_' . $id] : $data;
                    $data = $this->ipsclass->my_br2nl($data);
                    $form_element = $this->ipsclass->compiled_templates['skin_register']->field_textarea('field_' . $id, $data, $error);
                } else {
                    $data = isset($this->ipsclass->input['field_' . $id]) ? $this->ipsclass->input['field_' . $id] : $data;
                    $form_element = $this->ipsclass->compiled_templates['skin_register']->field_textinput('field_' . $id, $data, $error);
                }

                ${$ftype} .= $this->ipsclass->compiled_templates['skin_register']->field_entry($fields->field_names[$id], $fields->field_desc[$id], $form_element, $id, $error);
            }

            $this->page_title = $this->ipsclass->lang['registration_form'];
            $this->nav = array($this->ipsclass->lang['registration_form']);

            //-----------------------------------------
            // ERROR CHECK
            //-----------------------------------------

            if (isset($form_errors['general']) AND is_array($form_errors['general']) AND count($form_errors['general'])) {
                $this->output .= $this->ipsclass->compiled_templates['skin_register']->errors(implode("<br />", $form_errors['general']));
            }

            //-----------------------------------------
            // Other errors
            //-----------------------------------------

            $final_errors = array('username' => NULL, 'dname' => NULL, 'password' => NULL, 'email' => NULL);

            foreach (array('username', 'dname', 'password', 'email') as $thing) {
                if (isset($form_errors[$thing]) AND is_array($form_errors[$thing]) AND count($form_errors[$thing])) {
                    $final_errors[$thing] = implode("<br />", $form_errors[$thing]);
                }
            }

            $this->ipsclass->input['UserName'] = isset($this->ipsclass->input['UserName']) ? $this->ipsclass->input['UserName'] : '';
            $this->ipsclass->input['PassWord'] = isset($this->ipsclass->input['PassWord']) ? $this->ipsclass->input['PassWord'] : '';
            $this->ipsclass->input['EmailAddress'] = isset($this->ipsclass->input['EmailAddress']) ? $this->ipsclass->input['EmailAddress'] : '';
            $this->ipsclass->input['EmailAddress_two'] = isset($this->ipsclass->input['EmailAddress_two']) ? $this->ipsclass->input['EmailAddress_two'] : '';
            $this->ipsclass->input['PassWord_Check'] = isset($this->ipsclass->input['PassWord_Check']) ? $this->ipsclass->input['PassWord_Check'] : '';
            $this->ipsclass->input['members_display_name'] = isset($this->ipsclass->input['members_display_name']) ? $this->ipsclass->input['members_display_name'] : '';
            $this->ipsclass->input['time_offset'] = isset($this->ipsclass->input['time_offset']) ? $this->ipsclass->input['time_offset'] : '';
            $this->ipsclass->input['allow_member_mail'] = isset($this->ipsclass->input['allow_member_mail']) ? $this->ipsclass->input['allow_member_mail'] : '';
            $this->ipsclass->input['dst'] = isset($this->ipsclass->input['dst']) ? $this->ipsclass->input['dst'] : '';

            $this->output .= $this->ipsclass->compiled_templates['skin_register']->ShowForm(array('TEXT' => $this->ipsclass->lang['std_text'], 'coppa_user' => $coppa), $final_errors);

            //-----------------------------------------
            // Replace elements
            //-----------------------------------------

            if ($this->ipsclass->vars['bot_antispam'] == 'gd') {
                $this->output = str_replace("<!--{REG.ANTISPAM}-->", $this->ipsclass->compiled_templates['skin_register']->bot_antispam_gd($regid), $this->output);
            } else if ($this->ipsclass->vars['bot_antispam'] == 'gif') {
                $this->output = str_replace("<!--{REG.ANTISPAM}-->", $this->ipsclass->compiled_templates['skin_register']->bot_antispam($regid), $this->output);
            }

            if ($required_output != "") {
                $this->output = str_replace("<!--{REQUIRED.FIELDS}-->", "\n" . $required_output, $this->output);
            }

            if ($optional_output != "") {
                $this->output = str_replace("<!--{OPTIONAL.FIELDS}-->", $this->ipsclass->compiled_templates['skin_register']->optional_title() . "\n" . $optional_output, $this->output);
            }

            //-----------------------------------------
            // Time zone...
            //-----------------------------------------

            $this->ipsclass->load_language('lang_ucp');

            $offset = ( $this->ipsclass->input['time_offset'] != "" ) ? $this->ipsclass->input['time_offset'] : $this->ipsclass->vars['time_offset'];

            $time_select = "<select name='time_offset' class='forminput'>";

            foreach ($this->ipsclass->lang as $off => $words) {
                if (preg_match("/^time_([\d\.\-]+)$/", $off, $match)) {
                    $time_select .= $match[1] == $offset ? "<option value='{$match[1]}' selected='selected'>$words</option>" : "<option value='{$match[1]}'>$words</option>";
                }
            }

            $time_select .= "</select>";

            $this->output = str_replace("<!--{TIME_ZONE}-->", "\n" . $time_select, $this->output);

            //-----------------------------------------
            // Boxes checked?
            //-----------------------------------------

            $admin_checked = 'checked="checked"';

            if ($this->ipsclass->input['CODE'] == '02') {
                //-----------------------------------------
                // Form submitted...
                //-----------------------------------------

                if (!$this->ipsclass->input['allow_admin_mail']) {
                    $admin_checked = '';
                }
            }

            $member_checked = $this->ipsclass->input['allow_member_mail'] ? 'checked="checked"' : '';
            $dst_checked = $this->ipsclass->input['dst'] ? 'checked="checked"' : '';

            $this->output = str_replace("<!--[admin.checked]-->", $admin_checked, $this->output);
            $this->output = str_replace("<!--[member.checked]-->", $member_checked, $this->output);
            $this->output = str_replace("<!--[dst.checked]-->", $dst_checked, $this->output);

            //-----------------------------------------
            // Avatar on register? by BabyWolf
            //-----------------------------------------
            $avatar_gallery_html = $this->get_avatars_categories();
            $this->output = str_replace("<!--{AVATAR_CHOOSER}-->", $avatar_gallery_html, $this->output);

            //-----------------------------------------
            // Subscribe on register?
            //-----------------------------------------

            $all_currency = array();
            $def_currency = "";
            $subs = array();
            $subs_output = "";
            $desc_output = "";

            if ($this->ipsclass->vars['subsm_show_reg']) {
                $this->ipsclass->load_language('lang_subscriptions');

                //-----------------------------------------
                // Get currency buns!
                // Ok, we did that joke in another module and it
                // wasn't funny then
                //-----------------------------------------

                $this->ipsclass->DB->simple_construct(array('select' => '*', 'from' => 'subscription_currency'));
                $this->ipsclass->DB->simple_exec();

                while ($c = $this->ipsclass->DB->fetch_row()) {
                    $all_currency[$c['subcurrency_code']] = $c;

                    if ($c['subcurrency_default']) {
                        $def_currency = $c;
                    }
                }

                //-----------------------------------------
                // Get subscription packages
                //-----------------------------------------

                $sub_output = $this->ipsclass->compiled_templates['skin_register']->subsm_start($def_currency['subcurrency_code']);

                //-----------------------------------------
                // Enforcing?
                //-----------------------------------------

                if (!$this->ipsclass->vars['subsm_enforce']) {
                    $sub_output .= $this->ipsclass->compiled_templates['skin_register']->subsm_row('0', $this->ipsclass->lang['subsm_none'], '0.00', $this->ipsclass->lang['subsm_na']);
                }

                $this->ipsclass->DB->simple_construct(array('select' => '*', 'from' => 'subscriptions', 'order' => 'sub_cost'));
                $this->ipsclass->DB->simple_exec();

                while ($row = $this->ipsclass->DB->fetch_row()) {
                    $duration = $row['sub_length'];

                    if ($duration > 1) {
                        $duration .= ' ' . $this->ipsclass->lang['timep_' . $row['sub_unit']];
                    } else {
                        $duration .= ' ' . $this->ipsclass->lang['time_' . $row['sub_unit']];
                    }

                    $duration = ($row['sub_unit'] == 'x') ? $this->ipsclass->lang['no_expire'] : $duration;

                    $sub_output .= $this->ipsclass->compiled_templates['skin_register']->subsm_row($row['sub_id'], $row['sub_title'], sprintf("%.2f", $row['sub_cost'] * $def_currency['subcurrency_exchange']), $duration
                    );
                    $desc_output .= "\n subdesc[{$row['sub_id']}] = '" . str_replace("'", "\\'", str_replace("\n", '\n', $row['sub_desc'])) . "';";
                }

                $sub_output .= $this->ipsclass->compiled_templates['skin_register']->subsm_end();

                //-----------------------------------------
                // Parse 'n show
                //-----------------------------------------

                if ($sub_output) {
                    $this->output = str_replace('<!--{SUBS.MANAGER}-->', $sub_output, $this->output);
                    $this->output = str_replace('<!--{SUBS.JSCRIPT}-->', str_replace("\r", "", $desc_output), $this->output);
                }
            }

            if (USE_MODULES == 1) {
                $this->modules->register_class($this);
                $this->modules->on_register_form();
            }
        }
        function lost_password_start($errors="") {
            //-----------------------------------------
            // Do we have another URL that one needs
            // to visit to reset their password?
            //-----------------------------------------

            $this->login_method = $this->ipsclass->DB->build_and_exec_query(array('select' => '*', 'from' => 'login_methods', 'where' => 'login_enabled=1'));

            if ($this->login_method['login_maintain_url']) {
                $this->ipsclass->boink_it($this->login_method['login_maintain_url']);
                exit();
            }

            if ($this->ipsclass->vars['bot_antispam']) {
                //-----------------------------------------
                // Sort out the security code
                //-----------------------------------------

                $r_date = time() - (60 * 60 * 6);

                //-----------------------------------------
                // Remove old reg requests from the DB
                //-----------------------------------------

                $this->ipsclass->DB->do_delete('reg_antispam', "ctime < '$r_date'");

                //-----------------------------------------
                // Set a new ID for this reg request...
                //-----------------------------------------

                $regid = md5(uniqid(microtime()));

                if ($this->ipsclass->vars['bot_antispam'] == 'gd') {
                    //-----------------------------------------
                    // Get 6 random chars
                    //-----------------------------------------

                    $reg_code = strtoupper(substr(md5(mt_rand()), 0, 6));
                } else {
                    //-----------------------------------------
                    // Set a new 6 character numerical string
                    //-----------------------------------------

                    $reg_code = mt_rand(100000, 999999);
                }

                //-----------------------------------------
                // Insert into the DB
                //-----------------------------------------

                $this->ipsclass->DB->do_insert('reg_antispam', array(
                    'regid' => $regid,
                    'regcode' => $reg_code,
                    'ip_address' => $this->ipsclass->input['IP_ADDRESS'],
                    'ctime' => time(),
                ));
            }

            //--------------------------------------
            // Clean text code
            //--------------------------------------
            require_once( ROOT_PATH . "sources/portal_plugins/recent_topics.php" );
            $recent_topic = new ppi_recent_topics();
            $recent_topic->ipsclass =& $this->ipsclass;
            $this->page_title = $recent_topic->Bw_CutString($this->ipsclass->lang['lost_pass_form'],900);        

            $this->nav = array($this->ipsclass->lang['lost_pass_form']);        
            $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
            $domain = $protocol . '://' . $_SERVER['HTTP_HOST'];
            //var_dump( $this->ipsclass->vars['pumper_url']);exit();
            
            if($domain == $this->ipsclass->vars['pumper_url'] ){
                $this->ipsclass->load_template('skin_global');
                
                    $this->ipsclass->load_template('skin_pumper');
                    $link = "<a href='{$this->ipsclass->vars['pumper_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title = '{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['home_title']}</a>&raquo;<span>{$this->ipsclass->lang['forgot_password_title']}</span>";
                    //echo $this->ipsclass->compiled_templates['skin_pumper']->load_pathway_module($link);
                    //exit();
                    $this->ipsclass->vars['pumErrRegPathway'] = $this->ipsclass->compiled_templates['skin_pumper']->load_pathway_module($link);
               
               $this->output .= $this->ipsclass->compiled_templates['skin_global']->div_start_top();
            }
            //if()
            
            
            if ($errors != "") {
                $this->output .= $this->ipsclass->compiled_templates['skin_register']->errors($this->ipsclass->lang[$errors]);
            }
            
            $this->output .= $this->ipsclass->compiled_templates['skin_register']->lost_pass_form($regid);
            if($domain == $this->ipsclass->vars['pumper_url'] ){
                $this->output .= $this->ipsclass->compiled_templates['skin_global']->div_end_top();
            }
            if ($this->ipsclass->vars['bot_antispam'] == 'gd') {
                $this->output = str_replace("<!--{REG.ANTISPAM}-->", $this->ipsclass->compiled_templates['skin_register']->bot_antispam_gd($regid), $this->output);
            } else if ($this->ipsclass->vars['bot_antispam'] == 'gif') {
                $this->output = str_replace("<!--{REG.ANTISPAM}-->", $this->ipsclass->compiled_templates['skin_register']->bot_antispam($regid), $this->output);
            }
            //echo($this->output);exit();
        }
        function lost_password_end() {
        if ($this->ipsclass->vars['bot_antispam']) {
            //-----------------------------------------
            // Security code stuff
            //-----------------------------------------

            if ($this->ipsclass->input['regid'] == "") {
                $this->lost_password_start('err_reg_code');
                return;
            }

            $this->ipsclass->DB->simple_construct(array('select' => '*',
                'from' => 'reg_antispam',
                'where' => "regid='" . trim($this->ipsclass->txt_alphanumerical_clean($this->ipsclass->input['regid'])) . "'"
            ));

            $this->ipsclass->DB->simple_exec();

            if (!$row = $this->ipsclass->DB->fetch_row()) {
                $this->show_reg_form('err_reg_code');
                return;
            }

            if (trim($this->ipsclass->txt_alphanumerical_clean($this->ipsclass->input['reg_code'])) != $row['regcode']) {
                $this->lost_password_start('err_reg_code');
                return;
            }
        }

        //-----------------------------------------
        // Back to the usual programming! :o
        //-----------------------------------------

        if ($_POST['member_name'] == "" AND $_POST['email_addy'] == "") {
            $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_username'));
        }

        //-----------------------------------------
        // Check for input and it's in a valid format.
        //-----------------------------------------

        $member_name = trim(strtolower($this->ipsclass->input['member_name']));
        $email_addy = trim(strtolower($this->ipsclass->input['email_addy']));

        if ($member_name == "" AND $email_addy == "") {
            $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_username'));
        }

        //-----------------------------------------
        // Attempt to get the user details from the DB
        //-----------------------------------------

        if ($this->ipsclass->vars['ipbli_usertype'] == 'username') {
            if ($member_name) {
                $this->ipsclass->DB->simple_construct(array('select' => 'members_display_name, name, id, email, mgroup', 'from' => 'members', 'where' => "members_l_username='{$member_name}'"));
                $this->ipsclass->DB->simple_exec();
            } else if ($email_addy) {
                $this->ipsclass->DB->simple_construct(array('select' => 'members_display_name, name, id, email, mgroup', 'from' => 'members', 'where' => "email='{$email_addy}'"));
                $this->ipsclass->DB->simple_exec();
            }
        } else {
            // We don't use the 'email_addy' input if usertype is email
            $email_addy = "";

            $this->ipsclass->DB->simple_construct(array('select' => 'members_display_name, name, id, email, mgroup', 'from' => 'members', 'where' => "email='{$member_name}'"));
            $this->ipsclass->DB->simple_exec();
        }

        if (!$this->ipsclass->DB->get_num_rows()) {
            $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_such_user'));
        } else {
            $member = $this->ipsclass->DB->fetch_row();

            //-----------------------------------------
            // Is there a validation key? If so, we'd better not touch it
            //-----------------------------------------

            if ($member['id'] == "") {
                $this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_such_user'));
            }

            $validate_key = md5($this->ipsclass->make_password() . uniqid(mt_rand(), TRUE));

            //-----------------------------------------
            // Get rid of old entries for this member
            //-----------------------------------------

            $this->ipsclass->DB->do_delete('validating', "member_id={$member['id']} AND lost_pass=1");

            //-----------------------------------------
            // Update the DB for this member.
            //-----------------------------------------

            $db_str = array(
                'vid' => $validate_key,
                'member_id' => $member['id'],
                #'real_group'  => $member['mgroup'],
                'temp_group' => $member['mgroup'],
                'entry_date' => time(),
                'coppa_user' => 0,
                'lost_pass' => 1,
                'ip_address' => $this->ipsclass->input['IP_ADDRESS'],
            );

            // Are they already in the validating group?

            if ($member['mgroup'] != $this->ipsclass->vars['auth_group']) {
                $db_str['real_group'] = $member['mgroup'];
            }

            $this->ipsclass->DB->do_insert('validating', $db_str);

            //-----------------------------------------
            // Send out the email.
            //-----------------------------------------

            $this->email->get_template("lost_pass");

            $this->email->build_message(array(
                'NAME' => $member['members_display_name'],
                'THE_LINK' => $this->base_url_nosess . "?act=Reg&CODE=lostpassform&uid=" . $member['id'] . "&aid=" . $validate_key,
                'MAN_LINK' => $this->base_url_nosess . "?act=Reg&CODE=lostpassform",
                'EMAIL' => $member['email'],
                'ID' => $member['id'],
                'CODE' => $validate_key,
                'IP_ADDRESS' => $this->ipsclass->input['IP_ADDRESS'],
                    )
            );

            $this->email->subject = $this->ipsclass->lang['lp_subject'] . ' ' . $this->ipsclass->vars['board_name'];
            $this->email->to = $member['email'];
            $this->output = "";
            $this->email->send_mail();
            $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
            $domain = $protocol . '://' . $_SERVER['HTTP_HOST'];
            if($domain == $this->ipsclass->vars['pumper_url'] || $domain == $this->ipsclass->vars['forum_pumper_url']){
                $this->ipsclass->load_template('skin_global');
                if($domain == $this->ipsclass->vars['pumper_url']){
                    $this->ipsclass->load_template('skin_pumper');
                    $link = "<a href='{$this->ipsclass->vars['pumper_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title = '{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['home_title']}</a>&raquo;<span>{$this->ipsclass->lang['forgot_password_title']}</span>";
                    $this->ipsclass->vars['pumErrRegPathway'] = $this->ipsclass->compiled_templates['skin_pumper']->load_pathway_module($link);
                }
               $this->output .= $this->ipsclass->compiled_templates['skin_global']->div_start_top();
            }
            $this->output .= $this->ipsclass->compiled_templates['skin_register']->show_lostpasswait($member);
            if($domain == $this->ipsclass->vars['pumper_url'] || $domain == $this->ipsclass->vars['forum_pumper_url']){
                $this->output .= $this->ipsclass->compiled_templates['skin_global']->div_end_top();
            }
        }

        $this->page_title = $this->ipsclass->lang['lost_pass_form'];
    }

        function lost_password(){
        //-----------------------------------------
        // Back to the usual programming! :o
        //-----------------------------------------
        //echo "dfdfdf";
        
        $this->ipsclass->load_language('lang_register');
        if ($_POST['UserName'] == "" AND $_POST['Email'] == "") {
            echo "&errorStatus=1" ;
            echo "&messageStatus=" . $this->ipsclass->lang['username_or_email_empty'];
            exit();
            //$this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_username'));
        }

        //-----------------------------------------
        // Check for input and it's in a valid format.
        //-----------------------------------------

        $member_name = trim(strtolower($this->ipsclass->input['UserName']));
        $email_addy = trim(strtolower($this->ipsclass->input['Email']));

        if ($member_name == "" AND $email_addy == "") {
            //$this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_username'));
            echo "&errorStatus=1" ;
            echo "&messageStatus=" . $this->ipsclass->lang['username_or_email_empty'];
            exit();
        }

        //-----------------------------------------
        // Attempt to get the user details from the DB
        //-----------------------------------------
        
        if ($this->ipsclass->vars['ipbli_usertype'] == 'username') {
            if ($member_name) {
                $this->ipsclass->DB->simple_construct(array('select' => 'members_display_name, name, id, email, mgroup', 'from' => 'members', 'where' => "members_l_username='{$member_name}'"));
                $this->ipsclass->DB->simple_exec();
            } else if ($email_addy) {
                $this->ipsclass->DB->simple_construct(array('select' => 'members_display_name, name, id, email, mgroup', 'from' => 'members', 'where' => "email='{$email_addy}'"));
                $this->ipsclass->DB->simple_exec();
            }
        } else {
            // We don't use the 'email_addy' input if usertype is email
            $email_addy = "";

            $this->ipsclass->DB->simple_construct(array('select' => 'members_display_name, name, id, email, mgroup', 'from' => 'members', 'where' => "email='{$member_name}'"));
            $this->ipsclass->DB->simple_exec();
        }

        if (!$this->ipsclass->DB->get_num_rows()) {
            //$this->ipsclass->Error(array('LEVEL' => 1, 'MSG' => 'no_such_user'));
            if($member_name){
                echo "&errorStatus=1" ;
                echo "&messageStatus=" . $this->ipsclass->lang['username_not_exist'];
                exit();
            }
            if($email_addy){
                echo "&errorStatus=1" ;
                echo "&messageStatus=" . $this->ipsclass->lang['email_not_exist'];
                exit();
            }
            
        } else {
            $member = $this->ipsclass->DB->fetch_row();
            
            $validate_key = md5($this->ipsclass->make_password() . uniqid(mt_rand(), TRUE));

            //-----------------------------------------
            // Get rid of old entries for this member
            //-----------------------------------------

            $this->ipsclass->DB->do_delete('validating', "member_id={$member['id']} AND lost_pass=1");

            //-----------------------------------------
            // Update the DB for this member.
            //-----------------------------------------

            $db_str = array(
                'vid' => $validate_key,
                'member_id' => $member['id'],
                #'real_group'  => $member['mgroup'],
                'temp_group' => $member['mgroup'],
                'entry_date' => time(),
                'coppa_user' => 0,
                'lost_pass' => 1,
                'ip_address' => $this->ipsclass->input['IP_ADDRESS'],
            );

            // Are they already in the validating group?

            if ($member['mgroup'] != $this->ipsclass->vars['auth_group']) {
                $db_str['real_group'] = $member['mgroup'];
            }

            $this->ipsclass->DB->do_insert('validating', $db_str);

            //-----------------------------------------
            // Send out the email.
            //-----------------------------------------

            $this->email->get_template("lost_pass");

            $this->email->build_message(array(
                'NAME' => $member['members_display_name'],
                'THE_LINK' => $this->base_url_nosess . "?act=Reg&CODE=lostpassform&uid=" . $member['id'] . "&aid=" . $validate_key,
                'MAN_LINK' => $this->base_url_nosess . "?act=Reg&CODE=lostpassform",
                'EMAIL' => $member['email'],
                'ID' => $member['id'],
                'CODE' => $validate_key,
                'IP_ADDRESS' => $this->ipsclass->input['IP_ADDRESS'],
                    )
            );

            $this->email->subject = $this->ipsclass->lang['lp_subject'] . ' ' . $this->ipsclass->vars['board_name'];
            $this->email->to = $member['email'];

            $this->email->send_mail();

            //$this->output = $this->ipsclass->compiled_templates['skin_register']->show_lostpasswait($member);
        }

        //$this->page_title = $this->ipsclass->lang['lost_pass_form'];
        //$url = $this->ipsclass->vars['forum_url'];
        echo "&errorStatus=0" ;
        echo "&messageStatus=" . $this->ipsclass->lang['send_suucess'];
        exit();
    }
        
        //-------------------------------------------
    // Begin show_terms_read
    //-------------------------------------------
    function show_terms_read($reg_code_err_msg="", $short_register=0) {
        
        //-----------------------------------------
        // INIT
        //-----------------------------------------
        if ($reg_code_err_msg) {
            $this->ipsclass->input = array_merge($this->ipsclass->input, $_SESSION);
            $_POST['act'] = 'termsread';
            $_POST = array_merge($_POST, $_SESSION);
        }

        if ($this->ipsclass->vars['ipbli_usertype'] == 'email') {
            $this->ipsclass->input['UserName'] = $this->ipsclass->input['members_display_name'];
        }

        $form_errors = array();
        $coppa = ($this->ipsclass->input['coppa_user'] == 1) ? 1 : 0;
        $in_username = str_replace('|', '&#124;', $this->ipsclass->input['UserName']);
        $in_password = trim($this->ipsclass->input['PassWord']);
        $in_email = strtolower(trim($this->ipsclass->input['EmailAddress']));
        $banfilters = array();
        $members_display_name = trim($this->ipsclass->input['members_display_name']);
        //echo "dfdfdfdfd";
        //echo $in_password;exit();

        //-----------------------------------------
        // Load ban filters
        //-----------------------------------------

        $this->ipsclass->DB->simple_construct(array('select' => '*', 'from' => 'banfilters'));
        $this->ipsclass->DB->simple_exec();

        while ($r = $this->ipsclass->DB->fetch_row()) {
            $banfilters[$r['ban_type']][] = $r['ban_content'];
        }


        //-----------------------------------------
        // Custom profile field stuff
        //-----------------------------------------

        require_once( ROOT_PATH . 'sources/classes/class_custom_fields.php' );
        $fields = new custom_fields($this->ipsclass->DB);

        $fields->cache_data = $this->ipsclass->cache['profilefields'];

        $fields->init_data();
        $fields->parse_to_save(1);


        //-----------------------------------------
        // Remove multiple spaces in the username
        //-----------------------------------------

        $in_username = preg_replace("/\s{2,}/", " ", $in_username);
        //echo $in_username;exit();
        //-----------------------------------------
        // Remove linebreaks if set to
        //-----------------------------------------

        if ($this->ipsclass->vars['usernames_nobr']) {
            $in_username = $this->ipsclass->my_br2nl($in_username);
            $in_username = str_replace("\n", "", $in_username);
            $in_username = str_replace("\r", "", $in_username);

            $members_display_name = $this->ipsclass->my_br2nl($members_display_name);
            $members_display_name = str_replace("\n", "", $members_display_name);
            $members_display_name = str_replace("\r", "", $members_display_name);
        }

        //-----------------------------------------
        // Remove 'sneaky' spaces
        //-----------------------------------------

        if ($this->ipsclass->vars['strip_space_chr']) {
            // use hexdec to convert between '0xAD' and chr
            $in_username = str_replace(chr(160), ' ', $in_username);
            $in_username = str_replace(chr(173), ' ', $in_username);
            $in_username = str_replace(chr(240), ' ', $in_username);
            $members_display_name = str_replace(chr(160), ' ', $members_display_name);
            $members_display_name = str_replace(chr(173), ' ', $members_display_name);
            $members_display_name = str_replace(chr(240), ' ', $members_display_name);
        }

        //-----------------------------------------
        // Trim up..
        //-----------------------------------------

        $in_username = trim($in_username);

        //-----------------------------------------
        // Test unicode name too
        //-----------------------------------------

        $unicode_name = preg_replace_callback('/&#([0-9]+);/si', create_function('$matches', 'return chr($matches[1]);'), $in_username);
        $unicode_name = str_replace("'", '&#39;', $unicode_name);
        $unicode_name = str_replace("\\", '&#92;', $unicode_name);

        $unicode_dname = preg_replace_callback('/&#([0-9]+);/si', create_function('$matches', 'return chr($matches[1]);'), $members_display_name);
        $unicode_dname = str_replace("'", '&#39;', $unicode_dname);
        $unicode_dname = str_replace("\\", '&#92;', $unicode_dname);

        //-----------------------------------------
        // Check the email address
        //-----------------------------------------

        $in_email = $this->ipsclass->clean_email($in_email);

        if (!$in_email OR strlen($in_email) < 6) {
            echo "err_invalid_email";
            exit();
        }

        //-----------------------------------------
        // Test email address
        //-----------------------------------------
        // ---------------------------------------
        // Updated by hnmtuan on 06/08/2011
        // ---------------------------------------
        $this->ipsclass->input['EmailAddress'] = strtolower(trim($this->ipsclass->input['EmailAddress']));
        $this->ipsclass->input['EmailAddress_two'] = $this->ipsclass->input['EmailAddress'];
        //$this->ipsclass->input['EmailAddress_two'] = strtolower( trim($this->ipsclass->input['EmailAddress_two']) );
        //$this->ipsclass->input['EmailAddress']     = strtolower( trim($this->ipsclass->input['EmailAddress']) );

        if (preg_match("#[\;\#\n\r\*\'\"<>&\%\!\(\)\{\}\[\]\?\\/\s]#", $this->ipsclass->input['EmailAddress_two'])) {
            echo "reg_error_email_invalid";
            exit();
        }

        //-----------------------------------------
        // More unicode..
        //-----------------------------------------

        $len_u = preg_replace("/&#([0-9]+);/", "-", $in_username);
        $len_p = preg_replace("/&#([0-9]+);/", "-", $in_password);
        $len_d = preg_replace("/&#([0-9]+);/", "-", $members_display_name);

        //-----------------------------------------
        // Test dname
        //-----------------------------------------

        if ($this->ipsclass->vars['auth_allow_dnames']) {
            if (!$members_display_name OR strlen($len_d) < 3 OR strlen($len_d) > $this->ipsclass->vars['max_user_name_length']) {
                echo "reg_error_no_name";
                exit();
            }
        }

        $this->ipsclass->vars['username_characters'] = 0;
        if ($this->ipsclass->vars['username_characters']) {
            $check_against = preg_quote($this->ipsclass->vars['username_characters'], "/");

            if (!preg_match("/^[" . $check_against . "]+$/i", $_POST['UserName']) && $this->ipsclass->vars['ipbli_usertype'] == 'username') {
                echo "username_characters";
                exit();
            }

            if (!preg_match("/^[" . $check_against . "]+$/i", $_POST['members_display_name']) && $this->ipsclass->vars['auth_allow_dnames']) {
                echo "username_characters";
                exit();
            }
        }

        //-----------------------------------------
        // Check for errors in the input.
        //-----------------------------------------

        if (!$in_username OR strlen($len_u) < 4 OR strlen($len_u) > $this->ipsclass->vars['max_user_name_length']) {
            //$form_errors['username'][$this->ipsclass->lang['reg_error_username_none']] = $this->ipsclass->lang['reg_error_username_none'];
            echo "reg_error_username_none";
            exit();
        }

        if (!$in_password OR strlen($len_p) < 6 OR strlen($len_p) > $this->ipsclass->vars['max_user_name_length']) {
            //$form_errors['password'][$this->ipsclass->lang['reg_error_no_pass']] = $this->ipsclass->lang['reg_error_no_pass'];
            echo "reg_error_no_pass";
            exit();
        }


        //-----------------------------------------
        // CHECK 1: Any errors (missing fields, etc)?
        //-----------------------------------------				
//        if (count($form_errors)) {
//            $this->show_page($form_errors);
//            return;
//        }
        //-----------------------------------------
        // USERNAME: Is this name already taken?
        //-----------------------------------------

        $this->ipsclass->DB->cache_add_query('login_getmember', array('username' => strtolower($in_username)));
        $this->ipsclass->DB->cache_exec_query();

        if ($name_check['id']) {
            echo "reg_error_username_taken";
            exit();
        }

        //-----------------------------------------
        // USERNAME: Is this name already taken (display)?
        //-----------------------------------------

        if ($this->ipsclass->vars['auth_allow_dnames']) {
            if ($this->ipsclass->vars['auth_dnames_nologinname']) {
                $this->ipsclass->DB->build_query(array('select' => "members_display_name, id",
                    'from' => 'members',
                    'where' => "members_l_display_name='" . strtolower($in_username) . "'",
                    'limit' => array(0, 1)));

                $this->ipsclass->DB->exec_query();

                if ($this->ipsclass->DB->get_num_rows()) {
                    echo "d_reg_error_username_taken";
                    exit();
                }
            }
        }

        //-----------------------------------------
        // UNAME: Check for existing LOG IN name.
        //-----------------------------------------

        $this->ipsclass->DB->build_query(array('select' => "name, id",
            'from' => 'members',
            'where' => "name='" . strtolower($in_username) . "'",
            'limit' => array(0, 1)));
        $this->ipsclass->DB->exec_query();

        if ($this->ipsclass->DB->get_num_rows()) {
            echo "u_reg_error_taken";
            exit();
        }
        //-----------------------------------------
        // USERNAME: Special chars?
        //-----------------------------------------

        if ($unicode_name != $in_username) {
            $this->ipsclass->DB->cache_add_query('login_getmember', array('username' => $this->ipsclass->DB->add_slashes(strtolower($unicode_name))));
            $this->ipsclass->DB->cache_exec_query();

            $name_check = $this->ipsclass->DB->fetch_row();

            if ($name_check['id']) {
                echo "reg_error_username_taken";
                exit();
            }
        }

        //-----------------------------------------
        // USERNAME: GUEST
        //-----------------------------------------

        if (strtolower($in_username) == 'guest') {
            echo "reg_error_username_taken";
            exit();
        }

        //-----------------------------------------
        // USERNAME: Banned?
        //-----------------------------------------

        if (is_array($banfilters['name']) and count($banfilters['name'])) {
            foreach ($banfilters['name'] as $n) {
                $n = str_replace('\*', '.*', preg_quote($n, "/"));

                if ($n AND preg_match("/^{$n}$/i", $in_username)) {
                    echo "reg_error_username_taken";
                    exit();
                }
            }
        }

        //-----------------------------------------
        // DNAME
        //-----------------------------------------

        if ($this->ipsclass->vars['auth_allow_dnames']) {
            //-----------------------------------------
            // Illegal characters
            //-----------------------------------------

            if (preg_match("#[\[\];,\|]#", str_replace('&#39;', "'", str_replace('&amp;', '&', $unicode_dname)))) {
                echo "d_reg_error_chars";
                exit();
            }

            //-----------------------------------------
            // DNAME: Is this name already taken?
            //-----------------------------------------

            $this->ipsclass->DB->cache_add_query('general_get_by_display_name', array('members_display_name' => strtolower($members_display_name)));
            $this->ipsclass->DB->cache_exec_query();

            $name_check = $this->ipsclass->DB->fetch_row();

            if ($name_check['id']) {
                echo "d_reg_error_taken";
                exit();
            }

            //-----------------------------------------
            // DNAME: Check for existing LOG IN name.
            //-----------------------------------------

            if ($this->ipsclass->vars['auth_dnames_nologinname']) {
                $this->ipsclass->DB->build_query(array('select' => "members_display_name, id",
                    'from' => 'members',
                    'where' => "members_l_username='" . strtolower($members_display_name) . "'",
                    'limit' => array(0, 1)));

                $this->ipsclass->DB->exec_query();

                if ($this->ipsclass->DB->get_num_rows()) {
                    echo "d_reg_error_taken";
                    exit();
                }
            }

            //-----------------------------------------
            // DNAME: Special chars?
            //-----------------------------------------

            if ($unicode_dname != $members_display_name) {
                $this->ipsclass->DB->cache_add_query('general_get_by_display_name', array('members_display_name' => $this->ipsclass->DB->add_slashes(strtolower($unicode_dname))));
                $this->ipsclass->DB->cache_exec_query();

                $name_check = $this->ipsclass->DB->fetch_row();

                if ($name_check['id']) {
                    echo "d_reg_error_taken";
                    exit();
                }
            }


            //-----------------------------------------
            // DNAME: GUEST
            //-----------------------------------------

            if (strtolower($members_display_name) == 'guest') {
                echo "d_reg_error_taken";
                exit();
            }
        }

        //-----------------------------------------
        // Is this email addy taken? CONVERGE THIS??
        //-----------------------------------------

        if ($this->ipsclass->converge->converge_check_for_member_by_email($in_email) == TRUE) {
            echo "reg_error_email_taken";
            exit();
        }
        //echo "reg_error_email_taken";exit();
        //-----------------------------------------
        // Load handler...
        //-----------------------------------------

        require_once( ROOT_PATH . 'sources/handlers/han_login.php' );
        $this->han_login = new han_login();
        $this->han_login->ipsclass = & $this->ipsclass;
        $this->han_login->init();
        $this->han_login->email_exists_check($email);

        if ($this->han_login->return_code != 'METHOD_NOT_DEFINED' AND $this->han_login->return_code != 'EMAIL_NOT_IN_USE') {
            echo "reg_error_email_taken";
            exit();
        }

        //-----------------------------------------
        // Are they banned [EMAIL]?
        //-----------------------------------------

        if (is_array($banfilters['email']) and count($banfilters['email'])) {
            foreach ($banfilters['email'] as $email) {
                $email = str_replace('\*', '.*', preg_quote($email, "/"));

                if (preg_match("/^{$email}$/i", $in_email)) {
                    echo "reg_error_email_ban";
                    exit();
                }
            }
        }


        $coppa = $this->ipsclass->my_getcookie('coppa');


        //-----------------------------------------
        // Continue
        //-----------------------------------------
        if (is_array($var)) {
            $_SESSION = array_merge($_SESSION, array('coppa_user' => $this->ipsclass->input['coppa_user'],
                'UserName' => $this->ipsclass->input['UserName'],
                'members_display_name' => $this->ipsclass->input['members_display_name'],
                'day' => $this->ipsclass->input['day'],
                'month' => $this->ipsclass->input['month'],
                'year' => $this->ipsclass->input['year'],
                'EmailAddress' => $this->ipsclass->input['EmailAddress'],
                'EmailAddress_two' => $this->ipsclass->input['EmailAddress_two'],
                'PassWord' => $this->ipsclass->input['PassWord'],
                'PassWord_Check' => $this->ipsclass->input['PassWord_Check'],
                'country' => $this->ipsclass->input['country'],
                'city' => $this->ipsclass->input['city'],
                'district' => $this->ipsclass->input['district'],
                'ward' => $this->ipsclass->input['ward'],
                'address' => $this->ipsclass->input['address'],
                'siteLocation' => $this->ipsclass->input['siteLocation']
                    )
            );
        } else {
            $_SESSION = array('coppa_user' => $this->ipsclass->input['coppa_user'],
                'UserName' => $this->ipsclass->input['UserName'],
                'members_display_name' => $this->ipsclass->input['members_display_name'],
                'day' => $this->ipsclass->input['day'],
                'month' => $this->ipsclass->input['month'],
                'year' => $this->ipsclass->input['year'],
                'EmailAddress' => $this->ipsclass->input['EmailAddress'],
                'EmailAddress_two' => $this->ipsclass->input['EmailAddress_two'],
                'PassWord' => $this->ipsclass->input['PassWord'],
                'PassWord_Check' => $this->ipsclass->input['PassWord_Check'],
                'country' => $this->ipsclass->input['country'],
                'city' => $this->ipsclass->input['city'],
                'district' => $this->ipsclass->input['district'],
                'ward' => $this->ipsclass->input['ward'],
                'address' => $this->ipsclass->input['address'],
                'siteLocation' => $this->ipsclass->input['siteLocation']
            );
        }


        require_once( ROOT_PATH . 'sources/classes/recaptchalib.php' );
        $privatekey = $this->ipsclass->vars['go_recapcha_private_key'];
        $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

        if ($resp->is_valid == false) {
            echo "capcha_key_invalid";
            //echo "dddd";
            exit();
        }
        if ($resp->error != "") {
            echo "capcha_key_invalid";
            //echo "eeee";
            exit();
        }
        if ($resp->error != null) {
            echo "capcha_key_invalid";
            //echo "fff";
            exit();
        }

        // Set referer to redirect user back to their current page before they click on register link
        if (trim($this->ipsclass->input['referer']))
            $_SESSION['referer'] = $this->ipsclass->input['referer'];

        require_once( ROOT_PATH . 'sources/classes/class_custom_fields.php' );
        $fields = new custom_fields($this->ipsclass->DB);

        $fields->cache_data = $this->ipsclass->cache['profilefields'];
        //echo $this->ipsclass->cache['profilefields'];exit();
        $fields->init_data();
        $fields->parse_to_register();

        foreach ($fields->out_fields as $id => $data) {
            $_SESSION['field_' . $id] = $this->ipsclass->input['field_' . $id];
        }


        $cache = $this->ipsclass->DB->simple_exec_query(array('select' => '*', 'from' => 'conf_settings', 'where' => "conf_key='reg_rules'"));

        $text = $cache['conf_value'] ? $cache['conf_value'] : $cache['conf_default'];

        // Update by tuanhnm 
        // Check is short register state
        if ($short_register) {
            $result = $this->short_register();
            //echo $result + "dfdfdfdfdf";exit();
            if (trim($result) != "register_success") {
                //echo $result;
                exit();
            }
            //exit();
        }

        //$this->output .= $this->ipsclass->compiled_templates['skin_regportal']->show_terms($this->ipsclass->my_nl2br($text), $coppa);

        $this->page_title = $this->ipsclass->lang['page_title'];
        $this->nav = array($this->ipsclass->lang['page_title']);


        $this->page_title = $this->ipsclass->lang['page_title'];
        $this->nav = array($this->ipsclass->lang['page_title']);
        //echo $this->output;
        echo "register_success";
        exit();
    }

    //-------------------------------------------
    // Begin short_register
    //-------------------------------------------
    function short_register() {
        //-----------------------------------------
        // INIT
        //-----------------------------------------

        if ($this->ipsclass->vars['ipbli_usertype'] == 'email') {
            $this->ipsclass->input['UserName'] = $this->ipsclass->input['members_display_name'];
        }

        $form_errors = array();
        $coppa = ($this->ipsclass->input['coppa_user'] == 1) ? 1 : 0;
        $in_username = str_replace('|', '&#124;', $this->ipsclass->input['UserName']);
        $in_password = trim($this->ipsclass->input['PassWord']);
        $in_email = strtolower(trim($this->ipsclass->input['EmailAddress']));
        $banfilters = array();
        $members_display_name = trim($this->ipsclass->input['members_display_name']);

        //-----------------------------------------
        // Build up the hashes
        //-----------------------------------------

        $mem_group = $this->ipsclass->vars['member_group'];

        //-----------------------------------------
        // Are we asking the member or admin to preview?
        //-----------------------------------------

        if ($this->ipsclass->vars['reg_auth_type']) {
            $mem_group = $this->ipsclass->vars['auth_group'];
        } else if ($coppa == 1) {
            $mem_group = $this->ipsclass->vars['auth_group'];
        } else if ($this->ipsclass->vars['subsm_enforce']) {
            $mem_group = $this->ipsclass->vars['subsm_nopkg_group'];
        }

        $_mke_time = ( $this->ipsclass->vars['login_key_expire'] ) ? ( time() + ( intval($this->ipsclass->vars['login_key_expire']) * 86400 ) ) : 0;

        $member = array(
            'name' => strtolower($in_username),
            'members_l_username' => strtolower($in_username),
            'members_display_name' => $this->ipsclass->vars['auth_allow_dnames'] ? $members_display_name : $in_username,
            'members_l_display_name' => strtolower($this->ipsclass->vars['auth_allow_dnames'] ? $members_display_name : $in_username ),
            'member_login_key' => $this->ipsclass->converge->generate_auto_log_in_key(),
            'member_login_key_expire' => $_mke_time,
            'email' => $in_email,
            'mgroup' => $mem_group,
            'posts' => 0,
            'joined' => time(),
            'ip_address' => $this->ipsclass->ip_address,
            'time_offset' => $this->ipsclass->input['time_offset'],
            'view_sigs' => 1,
            'email_pm' => 1,
            'view_img' => 1,
            'view_avs' => 1,
            'restrict_post' => 0,
            'view_pop' => 1,
            'msg_total' => 0,
            'new_msg' => 0,
            'coppa_user' => $coppa,
            'language' => $this->ipsclass->vars['default_language'],
            'members_auto_dst' => 1,
            'members_editor_choice' => $this->ipsclass->vars['ips_default_editor'],
            'allow_admin_mails' => intval($this->ipsclass->input['allow_admin_mail']),
            'hide_email' => $this->ipsclass->input['allow_member_mail'] ? 0 : 1,
            'subs_pkg_chosen' => intval($this->ipsclass->input['subspackage'])
        );

        //-----------------------------------------
        // Insert: CONVERGE
        //-----------------------------------------

        $salt = $this->ipsclass->converge->generate_password_salt(5);
        $passhash = $this->ipsclass->converge->generate_compiled_passhash($salt, md5($in_password));

        $converge = array('converge_email' => $in_email,
            'converge_joined' => time(),
            'converge_pass_hash' => $passhash,
            'converge_pass_salt' => str_replace('\\', "\\\\", $salt)
        );

        //-----------------------------------------
        // Add Converge: Member
        //-----------------------------------------
        $this->han_login->create_account(array('email' => $member['email'],
            'joined' => $member['joined'],
            'password' => $in_password,
            'ip_address' => $this->ipsclass->ip_address
        ));

        if ($this->han_login->return_code != 'METHOD_NOT_DEFINED' AND $this->han_login->return_code != 'SUCCESS') {
            //$this->ipsclass->Error(array('LEVEL' => 5, 'MSG' => 'han_login_create_failed', 'EXTRA' => $this->han_login->return_details ? $this->han_login->return_details : $this->han_login->return_code));
            //return "han_login_create_failed";
            //exit();
        }

        $this->ipsclass->DB->do_insert('members_converge', $converge);

        //-----------------------------------------
        // Get converges auto_increment user_id
        //-----------------------------------------
        $member_id = $this->ipsclass->DB->get_insert_id();
        $member['id'] = $member_id;

        //-----------------------------------------
        // Insert: MEMBERS
        //-----------------------------------------
        $this->ipsclass->DB->force_data_type = array('name' => 'string', 'members_display_name' => 'string', 'members_l_username' => 'string', 'members_l_display_name' => 'string');

        $this->ipsclass->DB->do_insert('members', $member);

        //-----------------------------------------
        // Insert: MEMBER EXTRA
        //-----------------------------------------
        $this->ipsclass->DB->do_insert('member_extra', array('id' => $member_id,
            'vdirs' => 'in:Inbox|sent:Sent Items',
            'interests' => '',
            'signature' => '',
            'location' => $this->ipsclass->site_info['site_name']
        ));

        if ($_FILES['upload_avatar']['name'] != '' || $_FILES['upload_photo']['name'] != '') {
            require_once ROOT_PATH . "sources/lib/func_usercp.php";
            $this->lib = new func_usercp();
            $this->ipsclass->member = $member;
            $this->ipsclass->member['g_edit_profile'] = 1;
            $this->ipsclass->member['g_avatar_upload'] = 1;
            $this->lib->ipsclass = &$this->ipsclass;
        }

        if ($_FILES['upload_avatar']['name'] != '' || $_FILES['upload_photo']['name'] != '') {
            $this->ipsclass->member = $this->ipsclass->sess->authorise();
        }

        //-----------------------------------------
        // Insert into the custom profile fields DB
        //-----------------------------------------
        $this->ipsclass->DB->do_delete('pfields_content', 'member_id=' . $member['id']);

//        $this->ipsclass->DB->force_data_type = array();
//        foreach ($fields->out_fields as $_field => $_data) {
//            $this->ipsclass->DB->force_data_type[$_field] = 'string';
//        }

        $fields->out_fields['member_id'] = $member['id'];

        $this->ipsclass->DB->do_insert('pfields_content', $fields->out_fields);

        //-----------------------------------------
        // Use modules?
        //-----------------------------------------

        if (USE_MODULES == 1) {
            $this->modules->register_class($this);

            $member['password'] = trim($this->ipsclass->input['PassWord']);

            $this->modules->on_create_account($member);

            if ($this->modules->error == 1) {
                return;
            }

            $member['password'] = "";
        }

        //-----------------------------------------
        // Validation key
        //-----------------------------------------
        $validate_key = md5($this->ipsclass->make_password() . time());
        $time = time();

        if ($coppa != 1) {
            if (($this->ipsclass->vars['reg_auth_type'] == 'user' ) or
                    ($this->ipsclass->vars['reg_auth_type'] == 'admin') or
                    ($this->ipsclass->vars['reg_auth_type'] == 'admin_user')) {
                //-----------------------------------------
                // We want to validate all reg's via email,
                // after email verificiation has taken place,
                // we restore their previous group and remove the validate_key
                //-----------------------------------------

                $this->ipsclass->DB->do_insert('validating', array(
                    'vid' => $validate_key,
                    'member_id' => $member['id'],
                    'real_group' => $this->ipsclass->vars['subsm_enforce'] ? $this->ipsclass->vars['subsm_nopkg_group'] : $this->ipsclass->vars['member_group'],
                    'temp_group' => $this->ipsclass->vars['auth_group'],
                    'entry_date' => $time,
                    'coppa_user' => $coppa,
                    'new_reg' => 1,
                    'ip_address' => $member['ip_address']
                ));


                if ($this->ipsclass->vars['reg_auth_type'] == 'user' OR $this->ipsclass->vars['reg_auth_type'] == 'admin_user') {
                    $this->email->get_template("reg_validate");

                    $this->email->build_message(array(
                        'THE_LINK' => $this->base_url_nosess . "?act=Reg&CODE=03&uid=" . urlencode($member_id) . "&aid=" . urlencode($validate_key),
                        'NAME' => $member['members_display_name'],
                        'MAN_LINK' => $this->base_url_nosess . "?act=Reg&CODE=05",
                        'EMAIL' => $member['email'],
                        'ID' => $member_id,
                        'CODE' => $validate_key,
                            )
                    );

                    $this->email->subject = $this->ipsclass->lang['new_registration_email'] . $this->ipsclass->vars['board_name'];
                    $this->email->to = $member['email'];

                    $this->email->send_mail();

                    $this->output = $this->ipsclass->compiled_templates['skin_register']->show_authorise($member);
                } else if ($this->ipsclass->vars['reg_auth_type'] == 'admin') {
                    $this->output = $this->ipsclass->compiled_templates['skin_register']->show_preview($member);
                }

                if ($this->ipsclass->vars['new_reg_notify']) {

                    $date = $this->ipsclass->get_date(time(), 'LONG', 1);

                    $this->email->get_template("admin_newuser");

                    $this->email->build_message(array(
                        'DATE' => $date,
                        'MEMBER_NAME' => $member['members_display_name'],
                            )
                    );

                    $this->email->subject = $this->ipsclass->lang['new_registration_email1'] . $this->ipsclass->vars['board_name'];
                    $this->email->to = $this->ipsclass->vars['email_in'];
                    $this->email->send_mail();
                }

                $this->page_title = $this->ipsclass->lang['reg_success'];

                $this->nav = array($this->ipsclass->lang['nav_reg']);
            } else {
                //-----------------------------------------
                // We don't want to preview, or get them to validate via email.
                //-----------------------------------------

                $this->ipsclass->cache['stats']['last_mem_name'] = $member['members_display_name'];
                $this->ipsclass->cache['stats']['last_mem_id'] = $member['id'];
                // Added by BabyWolf
                $this->ipsclass->cache['stats']['last_mem_username'] = $member['name'];
                $this->ipsclass->cache['stats']['mem_count'] += 1;

                $this->ipsclass->update_cache(array('name' => 'stats', 'array' => 1, 'deletefirst' => 0));

                if ($this->ipsclass->vars['new_reg_notify']) {
                    $date = $this->ipsclass->get_date(time(), 'LONG', 1);

                    $this->email->get_template("admin_newuser");

                    $this->email->build_message(array(
                        'DATE' => $date,
                        'MEMBER_NAME' => $member['members_display_name'],
                            )
                    );

                    $this->email->subject = $this->ipsclass->lang['new_registration_email1'] . $this->ipsclass->vars['board_name'];
                    $this->email->to = $this->ipsclass->vars['email_in'];
                    $this->email->send_mail();
                }

                $this->ipsclass->no_print_header = 0;

                $this->ipsclass->my_setcookie("pass_hash", $member['member_login_key'], 1);
                $this->ipsclass->my_setcookie("member_id", $member['id'], 1);
                $this->ipsclass->my_setcookie('session_id', '0', -1);

                $this->ipsclass->stronghold_set_cookie($member['id'], $member['member_login_key']);
//                $this->ipsclass->boink_it($this->ipsclass->base_url);
//                exit();
//                $url = 'http://www.worldgames.vn/index.php?';
//                $act = 'regportal';
//                $code = 'wg';
//                echo "&errorStatus=0";
//                echo "&urlRegister=" . $url;
//                echo "&act=" . $act;
//                echo "&CODE=" . $code;
                //$this->ipsclass->boink_it($this->ipsclass->base_url . '&act=login&CODE=autologin&fromreg=1');
            }
        } else {
            // This is a COPPA user, so lets tell them they registered OK and redirect to the form.

            $this->ipsclass->DB->do_insert('validating', array(
                'vid' => $validate_key,
                'member_id' => $member['id'],
                'real_group' => $this->ipsclass->vars['member_group'],
                'temp_group' => $this->ipsclass->vars['auth_group'],
                'entry_date' => $time,
                'coppa_user' => $coppa,
                'new_reg' => 1,
                'ip_address' => $member['ip_address']
            ));

            //$url = $this->ipsclass->base_url;
//            $this->ipsclass->boink_it($this->ipsclass->base_url);
//            exit();
//            $act = 'regportal';
//            $code = 'wg';
//            echo "&errorStatus=0";
//            echo "&urlRegister=" . $url;
//            echo "&act=" . $act;
//            echo "&CODE=" . $code;
            //$this->ipsclass->print->redirect_screen($this->ipsclass->lang['cp_success'], 'act=Reg&amp;CODE=12');
        }
        return "register_success";
    }
    }

?>
